#!/usr/bin/env python
# -*- coding:utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json


class ItcastPipeline(object):
    # 该方法可选，类初始化
    def __init__(self):
        self.filename = open("teacher.json", "w")

    # 这个方法必须写，处理item数据
    def process_item(self, item, spider):
        jsontext = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.filename.write(jsontext.decode('gb2312').encode("utf-8"))

    # 这个方法可选
    def close_spider(self, spider):
        self.filename.close()
